#!/usr/bin/env bash
set -euo pipefail

USER_NAME="${SUDO_USER:-$USER}"

# Create tap0 owned by the current user and assign 10.0.0.1/24
if ip link show tap0 &>/dev/null; then
  echo "[=] tap0 already exists"
else
  echo "[*] Creating tap0 for user $USER_NAME ..."
  sudo ip tuntap add tap0 mode tap user "$USER_NAME"
fi

sudo ip addr flush dev tap0 || true
sudo ip addr add 10.0.0.1/24 dev tap0
sudo ip link set tap0 up

#开启三层转发
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -A FORWARD -i eth0 -o tap0 -j ACCEPT
sudo iptables -A FORWARD -i tap0 -o eth0 -j ACCEPT

echo "[✓] tap0 is up at 10.0.0.1/24"
